package com.gy.niuer.controller;


import com.gy.niuer.domain.Doctor;
import com.gy.niuer.service.DoctorService;
import com.gy.niuer.service.SchedulingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/doctor")
@CrossOrigin
public class DoctorController {

    @Autowired
    DoctorService doctorService;

    @Autowired
    SchedulingService schedulingService;

    @GetMapping("/getDoctor/{id}")
    public List<Doctor> getDoctor(@PathVariable int id){

        List<Doctor> doctorByDepartment = doctorService.getDoctorByDepartment(id);

        return doctorByDepartment;
    }

    @PostMapping("/thumbs")
    public String thumbsOneDoctor(@RequestParam("doctorId") int doctorId,@RequestParam("patientId") int patientId){

        boolean isSuccess = doctorService.thumbsOneDoctor(doctorId, patientId);

        if(isSuccess){
            return "点赞成功！！";
        }else{
            return "你已经给这位医生点过赞了哦。";
        }
    }

    @GetMapping("/getSortedDoctor")
    public List<Doctor> getAllDoctor(){
        List<Doctor> allDoctor = doctorService.getSortedDoctor();
        return allDoctor;
    }

    @PostMapping("/doctorLeave/{id}")
    public String doctorLeave(@PathVariable("id") int doctorId, Date time){

        boolean isFree = schedulingService.deleteScheduling(doctorId, time);//判断医生在该时段是否空闲
        if(isFree){
            return "成功给改医生批假！";
        }else{
            return "该医生在当前时段有病人预约了，无法批假。";
        }
    }

    @GetMapping("/isThumbed/{scheduling}")
    public List<Boolean> isThumbed(@PathVariable int scheduling, int patientId){

        int p = patientId;

        List<Doctor> doctors = doctorService.getDoctorByDepartment(scheduling);

        ArrayList<Boolean> booleans = new ArrayList<>();

        for (Doctor d :doctors) {
            booleans.add(doctorService.isThumbed(d.getThumbsId(),patientId));
        }

        return booleans;
    }

    @PostMapping("/addDoctor")
    public String addDoctor(@PathVariable Doctor doctor){

        doctorService.addDoctor(doctor);

        return "添加成功！";

    }

    @GetMapping("/getAllDoctor")
    public List<Doctor> getAll(){
        List<Doctor> allDoctor = doctorService.getAllDoctor();
        return allDoctor;
    }


}
